package com.fzq.flowable_demo.web;

import com.fzq.flowable_demo.service.ProcessDiagramService;
import com.fzq.flowable_demo.service.ProcessDiagramServiceFinish;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/process")
public class ProcessDiagramController {

    @Autowired
    private ProcessDiagramServiceFinish processDiagramServiceFinish;
    @Autowired
    private ProcessDiagramService processDiagramService;

    /**
     * 查看流程图
     */
    @GetMapping("/diagram/{processInstanceId}")
    public void getProcessDiagram(@PathVariable String processInstanceId,
                                  HttpServletResponse response) throws IOException {
        processDiagramServiceFinish.generateDiagram(response, processInstanceId);
    }

    /**
     * 查看已完成流程的流程图
     */
    @GetMapping("/diagram/completed/{processInstanceId}")
    public void getCompletedProcessDiagram(@PathVariable String processInstanceId,
                                           HttpServletResponse response) throws IOException {
        processDiagramServiceFinish.generateCompletedDiagram(response, processInstanceId);
    }


    /**
     * 查看流程图（支持运行中和已结束的流程）
     */
    @GetMapping("/diagram2/{processInstanceId}")
    public void getProcessDiagram2(@PathVariable String processInstanceId,
                                  HttpServletResponse response) {
        try {
            processDiagramService.generateDiagram(response, processInstanceId);
        } catch (IllegalArgumentException e) {
            // 参数错误
            response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
            try {
                response.getWriter().write("错误: " + e.getMessage());
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        } catch (Exception e) {
            // 其他错误
            response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            try {
                response.getWriter().write("生成流程图时发生错误: " + e.getMessage());
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }

    /**
     * 检查流程实例状态
     */
    @GetMapping("/status/{processInstanceId}")
    public ResponseEntity<Map<String, Object>> getProcessStatus(@PathVariable String processInstanceId) {
        Map<String, Object> result = new HashMap<>();

        try {
            boolean exists = processDiagramService.isProcessInstanceExists(processInstanceId);
            String status = processDiagramService.getProcessInstanceStatus(processInstanceId);

            result.put("exists", exists);
            result.put("status", status);
            result.put("processInstanceId", processInstanceId);
            result.put("success", true);

            return ResponseEntity.ok(result);
        } catch (Exception e) {
            result.put("success", false);
            result.put("error", e.getMessage());
            return ResponseEntity.badRequest().body(result);
        }
    }

}